package com.tk.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ZXCodeUtil {

    // 二维码颜色
    private static final int BLACK = 0xFF000000;
    // 二维码颜色
    private static final int WHITE = 0xFFFFFFFF;

    public static void main(String[] args) {
        generateCode("哈哈12345-hello-world|12345");
    }

    /*二维码生成*/
    public static byte[] generateCode(String text) {
        return generateCode(text, "png", 200, 200);
    }

    public static byte[] generateCode(String text, String format, int width, int height) {
        Map<EncodeHintType, Object> his = new HashMap<>();
        // 设置编码字符集
        his.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 错误纠正级别
        his.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 二维码边距
        his.put(EncodeHintType.MARGIN, 0);
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            // 1、生成二维码
            BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his);
            // 2、获取二维码宽高
            int codeWidth = encode.getWidth();
            int codeHeight = encode.getHeight();
            // 3、将二维码放入缓冲流
            BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < codeWidth; i++) {
                for (int j = 0; j < codeHeight; j++) {
                    // 4、循环将二维码内容定入图片
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
            }
            ImageIO.write(image, format, os);
            return os.toByteArray();
        } catch (Exception e) {
            log.error("ERROR ZXCodeUtil.generateCode 二维码生成失败 cause:", e);
        }
        return new byte[0];
    }

    /**
     * 生成条形码
     * @param data   扫描二维码后得到的信息
     * @param width  条形码的宽
     * @param height 条形码的高
     * @return image
     */
    public BufferedImage generateBarCode(String data, int width, int height) {
        BufferedImage image = null;
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            // 字符编码
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            MultiFormatWriter writer = new MultiFormatWriter();
            // 码的样式这次选CODE_128
            BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.CODE_128, width, height, hints);
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    image.setRGB(i, j, bitMatrix.get(i, j) ? 0 : 0xFFFFFF);   // 黑白条形码
                }
            }
        } catch (WriterException e) {
            log.error("ERROR ZXCodeUtil.generateBarCode 条形码生成失败 cause:", e);

        }

        return image;
    }

}

